﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace ZLDC.Common.Estate
{
    public class _ParseSQL
    {
        private string fieldList;
        private string tableName;
        private string whereClause;
        private string groupClause;
        private string havingClause;
        private string orderClause;

        private ArrayList KeyWordList;
        private ArrayList IndexList;

        public _ParseSQL()
        {
            fieldList = "";
            tableName = "";
            whereClause = "";
            groupClause = "";
            havingClause = "";
            orderClause = "";

            KeyWordList = new ArrayList();
            IndexList = new ArrayList();
        }

        public string FieldList
        {
            get
            {
                return fieldList;
            }
        }

        public string TableName
        {
            get
            {
                return tableName;
            }
        }

        public string WhereClause
        {
            get
            {
                return whereClause;
            }
        }

        public string HavingClause
        {
            get
            {
                return havingClause;
            }
        }

        public string OrderClause
        {
            get
            {
                return orderClause;
            }
        }

        public string GroupClause
        {
            get
            {
                return groupClause;
            }
        }

        public static string getTableName(string sSql)
        {
            string s = "";

            int i = sSql.IndexOf("FROM");
            int j = sSql.IndexOf("WHERE");
            if (i != -1 && j != -1)
            {
                s = sSql.Substring(i + 5, j - i - 6);
            }
            else
            {
                throw new Exception("取得表名失败！，请联系管理员！");
            }
            return s.Replace("NOLOCK", "").Trim();
        }

        public bool Parse(string strSQL)
        {
            int Index = 0;

            int len = 0;
            int StartIndex = 0;

            strSQL = strSQL.ToUpper();

            strSQL = strSQL.Trim();

            IndexList.Clear();

            Index = strSQL.IndexOf("SELECT");
            if (Index != -1)
            {
                IndexList.Add(Index);
            }

            Index = strSQL.IndexOf("FROM");
            if (Index != -1)
            {
                IndexList.Add(Index);
            }

            Index = strSQL.IndexOf("WHERE");
            if (Index != -1)
            {
                IndexList.Add(Index);
            }

            Index = strSQL.IndexOf("GROUP");
            if (Index != -1)
            {
                IndexList.Add(Index);
            }

            Index = strSQL.IndexOf("HAVING");
            if (Index != -1)
            {
                IndexList.Add(Index);
            }

            Index = strSQL.IndexOf("ORDER");
            if (Index != -1)
            {
                IndexList.Add(Index);
            }

            IndexList.Sort();
            KeyWordList.Clear();

            for (int i = 0; i < IndexList.Count; i++)
            {
                StartIndex = Convert.ToInt32(IndexList[i].ToString());
                if (i != IndexList.Count - 1)
                {
                    len = Convert.ToInt32(IndexList[i + 1].ToString()) - StartIndex;
                }
                else
                {
                    len = strSQL.Length - StartIndex;
                }
                KeyWordList.Add(strSQL.Substring(StartIndex, len));
            }
            bool bSelect = false; bool bFrom = false;
            bool bWhere = false;
            for (int i = 0; i < KeyWordList.Count; i++)
            {
                if (KeyWordList[i].ToString().IndexOf("SELECT") != -1 && bSelect == false)
                {
                    fieldList = _Tools.TrimBlank(KeyWordList[i].ToString());
                    fieldList = fieldList.Remove(0, 6);
                    bSelect = true;
                }
                else if (KeyWordList[i].ToString().IndexOf("FROM") != -1 && bFrom == false)
                {
                    tableName = KeyWordList[i].ToString().Split(' ')[1];
                    bFrom = true;
                }
                else if (KeyWordList[i].ToString().IndexOf("WHERE") != -1 && bWhere == false)
                {
                    whereClause = KeyWordList[i].ToString();
                    bWhere = true;
                }
                else if (KeyWordList[i].ToString().IndexOf("GROUP") != -1)
                {
                    groupClause = KeyWordList[i].ToString();
                }
                else if (KeyWordList[i].ToString().IndexOf("HAVING") != -1)
                {
                    havingClause = KeyWordList[i].ToString();
                }
                else if (KeyWordList[i].ToString().IndexOf("ORDER") != -1)
                {
                    orderClause = KeyWordList[i].ToString();
                }
            }

            return true;
        }

    }
}
